package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.enumerations.PowerDnsDomainType;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.IPowerDnsService;
import com.macasa.panel.entity.api.IPowerDnsServiceDomain;
import com.macasa.panel.entity.api.IPowerDnsServiceDomainRecord;

/**
 * Un domain de type master aura seulement l'attribue powerDnsService de remplis pour désigner le serveur qui l'héberge Un domain de type slave aura l'attribut
 * master de remplis pour désigner le serveur hôte qui héberge le domain de type master correspondant ainsi que l'attribut powerDnsService pour désigner le
 * serveur qui héberge le domain de type slave
 * 
 * @author Administrateur
 * 
 */
@Entity
@Table(name = "power_dns_service_domain", uniqueConstraints = { @UniqueConstraint(columnNames = { "name", "type" }) })
@SequenceGenerator(name = "PowerDnsServiceDomain_SEQ", sequenceName = "PowerDnsServiceDomainIdSeq", initialValue = 1)
@Getter
@Setter
@ToString
public class PowerDnsServiceDomain extends AbstractModel<Integer> implements IPowerDnsServiceDomain {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long					serialVersionUID	= -2672785839083552057L;

	@Id
	@Column(name = "power_dns_service_domain_id", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "PowerDnsServiceDomain_SEQ")
	private Integer								id;

	@Column(name = "name", nullable = false)
	private String								name;

	@Enumerated(EnumType.STRING)
	@Column(name = "type", nullable = false)
	private PowerDnsDomainType					type;

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.PowerDnsService.class)
	@JoinColumn(name = "fk_master", nullable = true, unique = false)
	@ForeignKey(name = "fk_power_dns_service_domain_02")
	private IPowerDnsService					master;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.PowerDnsService.class)
	@JoinColumn(name = "fk_power_dns_service_id", nullable = false)
	private IPowerDnsService					powerDnsService;

	@OneToMany(mappedBy = "powerDnsServiceDomain", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.PowerDnsServiceDomainRecord.class)
	@ForeignKey(name = "fk_power_dns_service_domain_record_01")
	private List<IPowerDnsServiceDomainRecord>	records;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PowerDnsServiceDomain)) {
			return false;
		}

		final PowerDnsServiceDomain other = CastHelpers.cast(obj, PowerDnsServiceDomain.class);

		return Objects.equal(this.id, other.id) && Objects.equal(this.type, other.type) && Objects.equal(this.master, other.master)
				&& Objects.equal(this.powerDnsService, other.powerDnsService) && Objects.equal(this.records, other.records);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.id, this.type, this.master, this.powerDnsService, this.records);
	}
}
